<?php
/**
 * Created by PhpStorm.
 * User: 李子昂
 * Date: 2019/2/14
 * Time: 15:16
 */
namespace App\ModelStatistic;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Input;

class NumberOfLevels extends Model{
    public function paginate()
    {
        $perPage = Request::get('per_page', 100);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;


        $tableAccount = 'tb_user_logout';
        //$data = \DB::connection($this->selectdb())->table($tableAccount)->skip($page* $perPage)->take($perPage)->get();
        $offset = ($page -1) * $perPage;
        $where = '';
        $cond = self::getCond($where);
        if (count($cond)!=0) {
            $data = \DB::connection($this->selectdb())->select(" select level ,count(*) as sum from ( select max(level) as `level` , player_Id from tb_user_logout WHERE 1= 1 {$where} GROUP BY player_id )as a group by level ", $cond);
            //$data = $data->toArray();
        }else{
            $data = [];
        }
        \Log::debug('duration deinc', [$where, $cond]);
        $total = count($data);//\DB::connection($this->selectdb())->table('player_info')->count();

        self::GenOtherData($data);

        $list = static::hydrate($data);
        \Log::debug('duration deinc hydrate:', [$list]);
        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }

    public function selectdb(){
        return 'remote_bgsi';
    }


    public static function getCond(&$where = ''){

        $cond = [];
        $begin = Request::get('start');
        $end = Request::get('end');
        $area = Request::get('area');

        $level = Request::get('level');
        if ($area == null) {
            return $cond;
        }
        if ($begin != null) {
            $where .= " AND reg_time between ? AND ? ";

            $cond[] = $begin;
            $cond[] = $end;
        }

        if ($level) {
            $where .= " AND level  <= ? ";
            $cond[] = $level;

        }

        if ($area) {
            $where .= " AND area IN (?)";
            $cond[] = $area;
        }

        return $cond;
    }

    public static function GenOtherData(&$data)
    {
        $total = 0; // 大于当前等级
        // 小于等于当前等级
        $less_level_data = [];
        foreach ($data as $obj) {
            $less_level_data[$obj->level] = $total;
            $total += $obj->sum;
        }
        \Log::debug(__METHOD__, [$total]);

        foreach ($data as &$obj) {
            $obj->rate_cur = round($obj->sum / $total * 100,  2);// . '%';
            $obj->rate_before = round($less_level_data[$obj->level]/ $total * 100, 2) ;
            // $obj->rate_before_label = round($less_level_data[$obj->level]/ $total * 100, 2). '%';
            $obj->rate_after = round(100 - $obj->rate_before, 2 );
        }
    }
}
